using System;
using System.Collections.Generic;
using System.Linq;
using Granite.Modeling;

namespace Rifts.Models.Characters
{
	public class SkillCollection : EditableObjectModelCollection<Skill>
	{
		public Skill FindByName(string skillName)
		{
			return this.SingleOrDefault(s => string.Equals(s.Name, skillName, StringComparison.InvariantCultureIgnoreCase));
		}

		public IEnumerable<Skill> FindAllByName(string skillName)
		{
			if (skillName.EndsWith("*", StringComparison.InvariantCultureIgnoreCase))
			{
				var nameStart = skillName.Substring(0, skillName.Length - 1); ;
				return this.Where(s => s.Name.StartsWith(nameStart, StringComparison.InvariantCultureIgnoreCase));
			}
			else
			{
				return this.Where(s => string.Equals(s.Name, skillName, StringComparison.InvariantCultureIgnoreCase));
			}
		}

		public bool HasHandToHand()
		{
			return this.Any(s => s.IsHandToHand);
		}

		public bool Contains(string skillName)
		{
			if (skillName.EndsWith("*", StringComparison.InvariantCultureIgnoreCase))
			{
				var nameStart = skillName.Substring(0, skillName.Length - 1); 
				return this.Any(s => s.Name.StartsWith(nameStart, StringComparison.InvariantCultureIgnoreCase));

			}
			else
			{
				return this.Any(s => string.Equals(s.Name, skillName, StringComparison.InvariantCultureIgnoreCase));
			}
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="skillName"></param>
		/// <returns></returns>
		/// <remarks>This will throw an exception if the skill isn't found.</remarks>
		public Skill this[string skillName]
		{
			get { return this.Single(s => string.Equals(s.Name, skillName, StringComparison.InvariantCultureIgnoreCase)); }
		}

	}
}
